package com.example.JavaWebApi.core.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Conditional(value = {MqttCondition.class})
@Slf4j
public class MqttConsumerCallBack extends AbstractMqttCallBack {


    public MqttConsumerCallBack(MqttConfig config) {
        super(config);
    }
    /**
     * 客户端断开连接的回调
     */
    @Override
    public void connectionLost(Throwable throwable) {
        log.error("mqtt connection lost");
    }

    /**
     * 消息到达的回调
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        log.info("接收消息主题:{}",topic);
        log.info("接收消息Qos:{}",message.getQos());
        log.info("接收消息内容:{}",new String(message.getPayload()));
        log.info("接收消息retained:{}",message.isRetained());
    }

    /**
     * 消息接收成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("接收消息成功");
    }
}
